<template>
  <Spin :spinning="loading" tip="Loading...">
  <div>
    <!-- 搜索 -->
    <!-- 紧急返件入出库管理 - 车型选择 -->
    <TopFilter :list="topFilterList" :cols="1" :collapse="collapse" labelWidth="90" @filterChange="changeHandle" :isSubmitBtn="false"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      selectionType="single"
      :columns="columns"
      :params="fetchParams"
      :dataSource="dataList"
      :fetchapi="fetchApi"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :click="addFn.bind()" icon="el-icon-plus">新 增</multiuse-button>
        <multiuse-button size="small" type="primary" :click="findFn.bind()">搜 索</multiuse-button>
        <multiuse-button size="small"  :click="blankFn.bind()">重 置</multiuse-button>
      </template>
    </FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
      <multiuse-button type="primary" :click="saveDialog.bind()">添加</multiuse-button>
    </div>
  </div>
  </Spin>
</template>
<script>
// import res from '@/mock/tableData';
import {getReList,saveRe} from '@/api/claim/factory/QualityInformationManagement_OEM/CoreIssueRules';
export default {
  // mixins: [dictionary],
  data() {
    return {
      loading:false,
      dataList: [],
      visible: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      topFilterList: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchApi: getReList,
      fetchParams: {}, //ipa 接口
      selectedRows: [] //选中经销商
    };
  },
  created() {

  },
  methods: {
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT', label: '收件人', fieldName: 'receiver', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '收件人电话', fieldName: 'receiverTel', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '收件地址', fieldName: 'receiverAddress', placeholder: '请输入', initialValue: ''},
        { type: 'INPUT', label: '返件仓库', fieldName: 'factory', placeholder: '请输入', initialValue: ''}
      ];
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val);
      console.log(val);
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '收件人', dataIndex: 'receiver', sorter: true },
        { title: '收件人电话', dataIndex: 'receiverTel', sorter: true },
        { title: '收件地址', dataIndex: 'receiverAddress', sorter: true },
        { title: '返件仓库', dataIndex: 'factory', sorter: true },
      ];
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    //保存
    saveDialog() {
      this.$emit('save', this.selectedRows);
    },
    //取消操作
    closeDialog() {
      this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
    },
    chooseOrderNo() {
      this.visible = true;
    },
    blankFn(){
      this.topFilterList.find(x => x.fieldName == 'receiver').initialValue='';
      this.topFilterList.find(x => x.fieldName == 'receiverTel').initialValue='';
      this.topFilterList.find(x => x.fieldName == 'receiverAddress').initialValue='';
      let val = {receiver:this.topFilterList.find(x => x.fieldName == 'receiver').initialValue,receiverTel:this.topFilterList.find(x => x.fieldName == 'receiverTel').initialValue,receiverAddress:this.topFilterList.find(x => x.fieldName == 'receiverAddress').initialValue}
      this.changeHandle(val);
    },
    findFn(){
      let val = {receiver:this.topFilterList.find(x => x.fieldName == 'receiver').initialValue,receiverTel:this.topFilterList.find(x => x.fieldName == 'receiverTel').initialValue,receiverAddress:this.topFilterList.find(x => x.fieldName == 'receiverAddress').initialValue}
      this.changeHandle(val);
    },
    async addFn(){
      let saveData = {};
      saveData.receiver = this.topFilterList.find(x => x.fieldName == 'receiver').initialValue;
      saveData.receiverTel = this.topFilterList.find(x => x.fieldName == 'receiverTel').initialValue;
      saveData.receiverAddress = this.topFilterList.find(x => x.fieldName == 'receiverAddress').initialValue;
      saveData.factory = this.topFilterList.find(x => x.fieldName == 'factory').initialValue;
      if (!this.topFilterList.find(x => x.fieldName == 'receiver').initialValue && !this.topFilterList.find(x => x.fieldName == 'receiverTel').initialValue && !this.topFilterList.find(x => x.fieldName == 'receiverAddress').initialValue && !this.topFilterList.find(x => x.fieldName == 'factory').initialValue){
        this.$notify.warning({title: '提示信息', message: '请将信息填写完整' || '保存失败!'});
      } else {
              this.loading = true;
              let res = await saveRe(saveData);
              if (res.resultCode === 200) {
                this.changeHandle();
                this.loading = false;
                return this.$notify.success({title: '提示信息', message: '保存成功'});
              } else {
                this.loading = false;
                // this.$notify.error({title: '提示信息', message: res.errMsg || '保存失败!'});
                // this.$emit('save');
              }
      }
    }
  }
  // mounted() {
  //   this.BaseTable = this.$refs.listModal.$refs.pageTable;
  // }
};
</script>

<style lang="less" scoped></style>
